package dbs

import (
	"ManageServer/utils/currenttime"
	"encoding/json"
	"github.com/jinzhu/gorm"
	"time"
)

type DbLog struct {
	ID        uint            `json:"id" gorm:"primary_key"`
	UserID    uint            `json:"user_id" gorm:"not null"`
	IP        string          `json:"ip" gorm:"type:varchar(32)"`
	Device    string          `json:"device" gorm:"type:varchar(255)"`
	Platform  string          `json:"platform" gorm:"type:varchar(32);not null"`
	LogType   string          `json:"log_type" gorm:"type:varchar(32)"`
	Module    string          `json:"module" gorm:"type:varchar(32)"`
	Content   json.RawMessage `json:"content" gorm:"type:varchar(255)"`
	CreatedAt time.Time       `json:"created_at" gorm:"type:datetime"`
}

type S_Log_Create struct {
	UserID   uint            `json:"user_id" validate:"required"`
	Platform string          `json:"platform"  validate:"required"`
	IP       string          `json:"ip" `
	Device   string          `json:"device" `
	Logtype  string          `json:"logtype" `
	Module   string          `json:"module" `
	Content  json.RawMessage `json:"content" `
}

type S_Log_List struct {
	UserName      string `json:"user_name"`
	UserId        uint   `json:"user_id"`
	UserIds       []uint `json:"user_ids"`
	DatetimeStart string `json:"datetime_start"`
	DatetimeEnd   string `json:"datetime_end"`
	Logtype       string `json:"logtype" `
	Module        string `json:"module" `
	Page          int    `json:"page" validate:"required" label:"页数"`
	Limit         int    `json:"limit" validate:"required" label:"每页显示数"`
	Order         string `json:"order"`
}

func (self *DbLog) Create() (err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db

		return conn.Create(&self).Error
	})
	return
}

func (self *DbLog) Query(s_list S_Log_List) (result []DbLog, err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db

		if s_list.UserId != 0 {
			conn = conn.Where("user_id = ?", s_list.UserId)
		}
		if len(s_list.UserIds) != 0 {
			conn = conn.Where("user_id in (?)", s_list.UserIds)
		}
		if s_list.DatetimeStart != "" && s_list.DatetimeEnd != "" {
			var datetimeStart, datetimeEnd time.Time
			datetimeStart, err = currenttime.StrToTime("2006-01-02 15:04:05", s_list.DatetimeStart)
			if err != nil {
				return
			}
			datetimeEnd, err = currenttime.StrToTime("2006-01-02 15:04:05", s_list.DatetimeEnd)
			if err != nil {
				return
			}
			conn = conn.Where("created_at between ? and ?", datetimeStart, datetimeEnd)
		}

		if s_list.Logtype != "" {
			conn = conn.Where("logtype = ?", s_list.Logtype)
		}
		if s_list.Module != "" {
			conn = conn.Where("module = ?", s_list.Module)
		}

		// 分页
		if s_list.Page != 0 && s_list.Limit != 0 {
			conn = conn.Offset((s_list.Page - 1) * s_list.Limit).Limit(s_list.Limit)
		}

		// 默认排序，按登录时间降序 = 按创建时间降序 = 按 id 降序
		s_list.Order = "id desc"
		if s_list.Order != "" {
			conn = conn.Order(s_list.Order)
		}

		return conn.Find(&result).Error
	})
	return
}

func (self *DbLog) Count(s_list S_Log_List) (count int, err error) {
	err = D_Pool(func(db **gorm.DB) (err error) {
		conn := *db

		if s_list.UserId != 0 {
			conn = conn.Where("user_id = ?", s_list.UserId)
		}
		if len(s_list.UserIds) != 0 {
			conn = conn.Where("user_id in (?)", s_list.UserIds)
		}
		if s_list.DatetimeStart != "" && s_list.DatetimeEnd != "" {
			var datetimeStart, datetimeEnd time.Time
			datetimeStart, err = currenttime.StrToTime("2006-01-02 15:04:05", s_list.DatetimeStart)
			if err != nil {
				return
			}
			datetimeEnd, err = currenttime.StrToTime("2006-01-02 15:04:05", s_list.DatetimeEnd)
			if err != nil {
				return
			}
			conn = conn.Where("created_at between ? and ?", datetimeStart, datetimeEnd)
		}

		if s_list.Logtype != "" {
			conn = conn.Where("logtype = ?", s_list.Logtype)
		}
		if s_list.Module != "" {
			conn = conn.Where("module = ?", s_list.Module)
		}

		return conn.Model(&self).Count(&count).Error
	})
	return
}
